package com.gadgets.framework.fileload.impl;

import com.gadgets.framework.common.util.I18nMessageUtils;
import com.gadgets.framework.common.util.PropertiesUtils;
import com.gadgets.framework.common.util.StringUtils;
import com.gadgets.framework.fileload.AbstractFileScanLoad;
import com.gadgets.framework.fileload.FileSuffixConstant;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-09-15
 *
 */
@Component
public class I18nFileSystemScanLoad extends AbstractFileScanLoad {

    public I18nFileSystemScanLoad() {
        super(FileSuffixConstant.I18N_SUFFIX);
    }

    /**
     * 分析文件（压缩文件）
     *
     * @param targetFile 目标文件
     * @param entry      文件内容
     */
    @Override
    public void analysisFile(ZipFile targetFile, ZipEntry entry) {
        try {
            InputStream inputStream = targetFile.getInputStream(entry);
            Properties properties = PropertiesUtils.getProperty(inputStream);
            Locale locale = getLocale(entry.getName());
            for (Map.Entry<Object, Object> i18nMsg : properties.entrySet()) {
                I18nMessageUtils.addI18nMsg(locale,i18nMsg.getKey().toString(),i18nMsg.getValue().toString());
            }
        } catch (IOException e) {
            logger.error("打开文件{}!{}的读取流失败，失败原因{}",targetFile.getName(),entry.getName(),e.getMessage());
            logger.error(StringUtils.formatExceptionStack(e));
        }
    }

    /**
     * 分析文件（非缩文件）
     *
     * @param file 目标文件
     */
    @Override
    public void analysisFile(File file) {
        try {
            Properties properties = PropertiesUtils.getProperty(file);
            Locale locale = getLocale(file.getName());
            for (Map.Entry<Object, Object> i18nMsg : properties.entrySet()) {
                I18nMessageUtils.addI18nMsg(locale,i18nMsg.getKey().toString(),i18nMsg.getValue().toString());
            }
        } catch (IOException e) {
            logger.error("打开文件{}的读取流失败，失败原因{}",file.getName(),e.getMessage());
            logger.error(StringUtils.formatExceptionStack(e));
        }
    }

    /**
     * 获取文件本地化信息
     * @param fileName  文件名
     * @return          本地化
     */
    protected Locale getLocale(String fileName) {
        int start = fileName.indexOf("_") + 1;
        int end = fileName.length() - fileSuffix.length();
        String localeStr = null;
        if (start > 0) {
            localeStr = fileName.substring(start, end);
        }
        Locale locale = Locale.getDefault();
        if (localeStr != null && localeStr.length() > 0) {
            String[] loc = localeStr.split("_");
            if (loc.length >= 2) {
                //取最后两位
                locale = new Locale(loc[loc.length - 2], loc[loc.length - 1]);
            } else {
                locale = new Locale(loc[0]);
            }
        }
        return locale;
    }

    /**
     * 最终文件处理
     */
    @Override
    public void analysisFinally() {

    }
}
